--[[--------------------------------------------------
CodePage.lua
Authors: YuriNB, VladVRO, mozers
Version: 2.5.1
------------------------------------------------------
Hybrid 2 scripts:
win1251 to cp936 keyboard mapper (YuriNB icq#2614215)
 Switch encoding of the current input and display win1251/dos936
 Simultaneous encoding of the console switches
and
codepage.lua (VladVRO)
 Show the current encoding in the status bar.

In addition, the script tries to distinguish from win1251 encoding cp936 when opening a file or if you do it.
If this fails, then automatically switches the display and entry.
------------------------------------------------------
Connection:
 In file SciTEStartup.lua add a line:
    dofile (props["SciteDefaultHome"].."\\tools\\CodePage.lua")
 Set in a file .properties:
    command.name.29.*=DOS Mode (cp936)
    command.29.*=change_codepage_zh_CN
    command.checked.29.*=$(code.page.936)
    command.mode.29.*=subsystem:lua,savebefore:no

    code.page.936.detect=1
--]]--------------------------------------------------

local function UpdateStatusCodePage(mode)
	local code_page_name = props["editor.code.page.name"]
	props["code.page.936"]='0'
	if mode == IDM_ENCODING_UCS2BE then
		props["editor.code.page.name"]='UTF-16 BE'
	elseif mode == IDM_ENCODING_UCS2LE then
		props["editor.code.page.name"]='UTF-16 LE'
	elseif mode == IDM_ENCODING_UTF8 then
		props["editor.code.page.name"]='UTF-8 BOM'
	elseif mode == IDM_ENCODING_UCOOKIE then
		props["editor.code.page.name"]='UTF-8'
	else
		if props["character.set"]=='255' then
			props["editor.code.page.name"]='DOS-936'
			props["code.page.936"]='1'
		elseif props["character.set"]=='234' then
			props["editor.code.page.name"]='GBK2312'
		elseif props["character.set"]=='236' then
			props["editor.code.page.name"]='BIG5'
		elseif tonumber(props["character.set"])==0 then
			props["editor.code.page.name"]='CP1252'
		elseif props["character.set"]=='238' then
			props["editor.code.page.name"]='CP1250'
		elseif props["character.set"]=='161' then
			props["editor.code.page.name"]='CP1253'
		elseif props["character.set"]=='162' then
			props["editor.code.page.name"]='CP1254'
		else
			props["editor.code.page.name"]='ANSI'
		end
	end
	
	if props["editor.code.page.name"] == code_page_name then return end

	props["editor.code.page"]=editor:codepage()
	scite.CheckMenus()
	scite.UpdateStatusBar()
end

local function CharsetDetect()
	if tonumber(props["code.page.936.detect"]) ~= 1 then return false end
	function CharsetDOS()
		local a, b
		a = editor:findtext("[\128-\175][\128-\175][\128-\175]", SCFIND_REGEXP, 0)
		if a then
			b = editor:findtext("[\240-\255][\240-\255][\240-\255]", SCFIND_REGEXP, 0)
			if not b then
				b = editor:findtext("[\192-\233][\192-\233][\192-\233]", SCFIND_REGEXP, 0)
			end
			if b and b < a then return false end
			return true
		end
		return false
	end
	if tonumber(props["editor.unicode.mode"]) == IDM_ENCODING_DEFAULT then
		if (props["character.set"]=='232' and CharsetDOS())
			or (props["character.set"]=='255' and not CharsetDOS()) then
			change_codepage_zh()
			return true
		end
	end
	return false
end

-- Add your event handler OnSwitchFile
AddEventHandler("OnSwitchFile", function(file)
	if not CharsetDetect() then
		UpdateStatusCodePage(tonumber(props["editor.unicode.mode"]))
	end
end)

-- Add your event handler OnOpen
AddEventHandler("OnOpen", function(file)
	if not CharsetDetect() then
		UpdateStatusCodePage(tonumber(props["editor.unicode.mode"]))
	end
end)

-- Add your event handler OnMenuCommand
AddEventHandler("OnMenuCommand", function(cmd, source)
	if cmd > 149 and cmd < 155 then -- IDM_ENCODING_DEFAULT, IDM_ENCODING_UCS2BE, IDM_ENCODING_UCS2LE, IDM_ENCODING_UTF8, IDM_ENCODING_UCOOKIE
		UpdateStatusCodePage(cmd)
	end
end)

-------------------------------------------------------------
-- win1251 to cp936 keyboard mapper
-------------------------------------------------------------

function change_codepage_zh()
	scite.MenuCommand(IDM_ENCODING_DEFAULT)
	if props["character.set"]=='255' then
		props["character.set"]='232'
	else
		props["character.set"]='255'
	end
	scite.Perform('reloadproperties:')
	UpdateStatusCodePage()
end

--[[--------------------------------------------------
-- Add your event handler OnChar
AddEventHandler("OnChar", function(char)
	if props["character.set"]=='255' then
		props["character.set"]='232'
	end
end)
--]]--------------------------------------------------